package demo002

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

func add(n int) int {
	return n + 1
}

type Monster struct {
	Name  string
	Age   int
	Skill string
}

func (m *Monster) Sotre() bool {
	data, err := json.Marshal(m)
	if err != nil {
		fmt.Println("Marshal err=", err)
		return false
	}
	filePath := "/Users/jim/Workdata/testgin0012/file/demo001/data2.txt"
	err = ioutil.WriteFile(filePath, data, 0666)
	if err != nil {
		fmt.Println("write file err=", err)
		return false
	}
	return true
}

func (m *Monster) Restore() bool {
	filePath := "/Users/jim/Workdata/testgin0012/file/demo001/data2.txt"
	buff, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println("ReadFile err=", err)
		return false
	}
	err = json.Unmarshal(buff, m)
	if err != nil {
		fmt.Println("Unmarshal err=", err)
		return false
	}
	return true
}
